﻿using FTBS.Models.BLO;
using FTBS.Models.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PagedList;

namespace FTBS.Controllers
{
    public class CategoryController : Controller
    {
        CategoryBLO _bloCategory = new CategoryBLO();

        public ActionResult Index(string sortOrder, string currentFilter, string searchString, int? page, bool? isSortDescending)
        {
            if (this.Session["RoleName"] == null ||this.Session["UserName"] == null || this.Session["Account"] == null || this.Session["RoleName"].ToString().Trim() != "Staff")
            {
                return Redirect("/Home/Announcement");
            }
            List<Category> listCategory = new List<Category>();
            listCategory = _bloCategory.LoadCategories();

            ViewBag.NameSortParm = "category_desc";
            ViewBag.CurrentSort = sortOrder;

            if (searchString != null)
            {
                page = 1;
            }
            else
            {
                searchString = currentFilter;
            }
            ViewBag.CurrentFilter = searchString;
            ViewBag.IsSortDes = false;

            if (!String.IsNullOrEmpty(searchString))
            {
                listCategory = listCategory.Where(category => category.CategoryName.ToUpper().Contains(searchString.ToUpper())).ToList();
            }

            switch (sortOrder)
            {
                case "category_desc":
                    if (isSortDescending == true)
                    {
                        listCategory = listCategory.OrderByDescending(category => category.CategoryName).ToList();
                        ViewBag.IsSortDes = false;
                    }
                    else
                    {
                        listCategory = listCategory.OrderBy(category => category.CategoryName).ToList();
                        ViewBag.IsSortDes = true;
                    }
                    break;
                default:
                    listCategory = listCategory.OrderBy(category => category.CategoryId).ToList();
                    ViewBag.IsSortDes = false;
                    break;
            }

            int pageSize = 5;
            int pageNumber = (page ?? 1);

            return View(listCategory.ToPagedList(pageNumber, pageSize));
        }

        public ActionResult Create()
        {
            if (this.Session["RoleName"] == null || this.Session["UserName"] == null || this.Session["Account"] == null || this.Session["RoleName"].ToString().Trim() != "Staff")
            {
                return Redirect("/Home/Announcement");
            }
            return View();
        }

        [HttpPost]
        public ActionResult Create(Category cate)
        {
            if (this.Session["RoleName"] == null || this.Session["UserName"] == null || this.Session["Account"] == null || this.Session["RoleName"].ToString().Trim() != "Staff")
            {
                return Redirect("/Home/Announcement");
            }
            if (ModelState.IsValid)
            {
                if (!_bloCategory.CheckIsExisted(cate))
                {
                    ViewBag.categoryName = null;
                    _bloCategory.CreateCategory(cate);
                    return RedirectToAction("Index");
                }
                ViewBag.cateName = cate.CategoryName;
                return View(cate);
            }
            if (_bloCategory.CheckIsExisted(cate))
            {
                ViewBag.cateName = cate.CategoryName;
            }
            return View(cate);
        }

        public ActionResult Edit(int id = 0)
        {
            if (this.Session["RoleName"] == null || this.Session["UserName"] == null || this.Session["Account"] == null || this.Session["RoleName"].ToString().Trim() != "Staff")
            {
                return Redirect("/Home/Announcement");
            }
            ViewBag.cateName = null;
            Category cate = _bloCategory.LoadCategoryById(id);
            if (cate == null)
            {
                return HttpNotFound();
            }
            return View(cate);
        }

        [HttpPost]
        public ActionResult Edit(Category cate)
        {
            if (this.Session["RoleName"] == null || this.Session["UserName"] == null || this.Session["Account"] == null || this.Session["RoleName"].ToString().Trim() != "Staff")
            {
                return Redirect("/Home/Announcement");
            }
            var id = cate.CategoryId;
            if (ModelState.IsValid)
            {
                if (!_bloCategory.CheckIsExisted(cate))
                {
                    ViewBag.cateName = null;
                    _bloCategory.EditCategory(cate);
                    return RedirectToAction("Index");
                }
                if (cate.CategoryName.Equals(_bloCategory.GetCategoryById(id).CategoryName))
                {
                    return RedirectToAction("Index");
                }
                ViewBag.cateName = cate.CategoryName;
                return View(cate);
            }
            ViewBag.cateName = cate.CategoryName;
            if (_bloCategory.CheckIsExisted(cate) && cate.CategoryName.Equals(_bloCategory.GetCategoryById(id).CategoryName))
            {
                ViewBag.cateName = null;
            }
            return View(cate);
        }

        [HttpDelete, ActionName("Delete")]
        public ActionResult Delete(int id = 0)
        {
            if (this.Session["RoleName"] == null || this.Session["UserName"] == null || this.Session["Account"] == null || this.Session["RoleName"].ToString().Trim() != "Staff")
            {
                return Redirect("/Home/Announcement");
            }
            Category category = _bloCategory.LoadCategoryById(id);
            if (category == null)
            {
                return HttpNotFound();
            }
            _bloCategory.DeleteCategory(category);
            return RedirectToAction("Index");
        }
    }
}
